-- FourthTips 主界面右下角Tips

local ClassRef = app.mvc.createPanelClass(...)

local btnNum = 4 -- tips个数上限

function ClassRef.create(node)
	local sprite = ClassRef.extend(node)
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._dataList = {} -- 数据队列
	self._nodeList = {} -- 节点队列

	for i = 1 , btnNum do
		self:_initTipsNode( i )
	end
end

function ClassRef:checkGuide()
	if UD:isGuideOver() then return end
	if not self:isVisible() then return end

	local params = {
		scene = Constant.FUNC_TYPE_DESKTOP_TIP,
		sceneSelf = self,
		getFunc = self.getGuideNodeFunc,
		nextFunc = self.checkGuide
	}
	EM:notify("checkGuide", params)
end

function ClassRef:getGuideNodeFunc(id)
	if id == 103 then
		-- 右下角快捷提示, 获取第一个tip，用于引导
		local node = self._nodeList[1]
		if not node then
			self:_initTipsNode( 1 )
		end
		if node:isVisible() then
			return node:getChildByName("Panel")
		end
	end
end

function ClassRef:_initTipsNode( idx )
	local nodeTemp = cc.CSLoader:createNode( Res.FourthLevelTipsNode )
	display.setCascadeOpacityEnabled( nodeTemp, true )
	nodeTemp:setVisible( false )
	display.uiAddClick(nodeTemp:getChildByName("Panel"), function()
        if BattleManager:isBattle() or StoryManager:isEnterStoryMode() then -- 战斗、剧情中
            return
        end

		local params = self._dataList[idx]
		if params then -- 有数据？
			self:_handlerSpecificLogic( params )
			local remindConf = GD:getRemindConfById( params.remindId )
			if 1 ~= tonumber( remindConf.timeRemain ) then -- 不需要保留？
				self:removeData( params )
			end
		end
	end)
	self._ui.Panel:addChild( nodeTemp )
	nodeTemp:setPosition( self._ui.Panel:getChildByName( "node_" .. idx ):getPosition() )
	self._nodeList[idx] = nodeTemp
end

-- 处理具体模块逻辑
function ClassRef:_handlerSpecificLogic( params )
	if Constant.FUNC_TYPE_TEAM_INVITE == params.remindId then -- 组队邀请？
		app:sendMsg("NewTeamMemberController", "beingInvited", params)
	elseif Constant.FUNC_TYPE_PLAYER_UP == params.remindId then -- 升级？
		app:sendMsg("PlayerController", "showPlayerLvUpView", params.dataTemp)
	elseif Constant.FUNC_TYPE_ADD_FRIEND == params.remindId then -- 请求添加好友
		app:sendMsg("FriendController", "notifyAddFriend", params.dataTemp)
	elseif Constant.FUNC_TYPE_GUILD_INVITE == params.remindId then -- 邀请入会
		app:sendMsg("NewguildController", "notiftGuildInvite", params.dataTemp)
	elseif Constant.FUNC_TYPE_DYNASTY_PROMOTE == params.remindId then
		app:sendMsg("NewguildController", "showDynastyPromoteView", params.dataTemp)
	elseif Constant.FUNC_TYPE_DYNASTY_HISTORY == params.remindId then
		app:sendMsg("NewguildController", "showDynastyHistoryView", params.dataTemp)
	elseif Constant.FUNC_TYPE_DYNASTY_CHELLENGE_LIST == params.remindId then
		app:sendMsg("BiddingController", "notifyChellengeList", params.dataTemp)
	elseif Constant.FUNC_TYPE_DYNASTY_FIRST == params.remindId then
		app:sendMsg("BiddingController", "notifyFirstChellenge", params.dataTemp)
	elseif Constant.FUNC_TYPE_CHAMPION == params.remindId then
		app:sendMsg("PvpChampionController", "openNoticeView", params.dataTemp)
    elseif Constant.FUNC_TYPE_TEAM_APPLY == params.remindId then --申请入队
		app:sendMsg("NewTeamApplyController", "openView", { 2 } )
	elseif Constant.FUNC_TYPE_TASK_ROADHERO == params.remindId then -- 英雄之路
		app:sendMsg("TaskController", "showHeroRoadNoticeView", params.dataTemp)
	elseif Constant.FUNC_TYPE_CHAT == params.remindId then --密聊新消息
		app:sendMsg("IMRoomController", "showView", params.dataTemp)
	elseif Constant.FUNC_TYPE_ACTIVITY_DUNGEONS_ASHINVADE == params.remindId then --灰烬入侵结果
		app:sendMsg("NewActivityDungeonsModel", "processDungeonsEnd", params.dataTemp, true)
	elseif Constant.FUNC_TYPE_NEWSPAPER == params.remindId then -- 无尽日报
		HistoryManager:reqGetNewspaperInfo(params.dataTemp)
	elseif Constant.FUNC_TYPE_CS_GUILD_WAR == params.remindId then -- 跨服工会战
		app:sendMsg("CrossServerController", "popGuildWarFightOpenTip")
	else
		print( " ------------>>>> FourthTips:_handlerSpecificLogic 未知模块Id？", params.remindId )
	end
end

-- 根据数据队列刷新显示
function ClassRef:_refreshPanel()
	for i = 1, btnNum do
		local nodeTemp = self._nodeList[i]
		if not nodeTemp then
			self:_initTipsNode( i )
		end
		local params = self._dataList[i]
		if params then
			nodeTemp:setVisible( true )
			nodeTemp:getTimeline():gotoFrameAndPlay( 0, true )
			local remindConf = GD:getRemindConfById( params.remindId )
			-- 底图
			-- local bgPath = GD:getImagePath( Res.remindIconPath, remindConf.bgPath )
			-- nodeTemp:findChild( "Panel/bgImg" ):loadTexture( bgPath )

			-- 具体提示的icon
			local iconPath = GD:getImagePath( Res.remindIconPath, remindConf.icon )
			nodeTemp:findChild( "Panel/img" ):loadTexture( iconPath )

			-- 配置了帧动画特效
			if nodeTemp:findChild("Panel/effectNode") then
				nodeTemp:getChildByName( "Panel" ):removeChildByName( "effectNode" )	
			end
			if remindConf.effect and "" ~= remindConf.effect then 
				local amiNode = cc.CSLoader:createNode( "ui2/main/" .. remindConf.effect .. ".csb" )
				if amiNode then
					amiNode:setName( "effectNode" )
					nodeTemp:getChildByName( "Panel" ):addChild( amiNode, -1 )
					amiNode:getTimeline():gotoFrameAndPlay( 0, true )
				end
			end
		else
			nodeTemp:setVisible( false )
		end
	end

	if #self._dataList > 0 then
		self:checkGuide()
	end
end

-- 插入数据
-- params = {
-- 	remindId = ,
-- 	dataTemp = -- 相关模块具体数据
-- }
function ClassRef:insertData( params )
	-- 密聊的要先判断是否需要插入tips(因为插入动作延迟1秒，在这一秒中可能已经切换到了当前密聊页签，所以需要在此再判断一遍)
	if params.remindId == Constant.FUNC_TYPE_CHAT then
		local model = app:getInst("IMModel")
		if model then
			local curChannel = model:getCurChannelIdx()
			local curTarget = model:getCurP2PTarget()
			if curChannel == 6 and curTarget and curTarget.uid == params.uid then
				return
			end
		end
	end
	-- 保证 添加好友 和 邀请入会 和 组队申请 的唯一, 有重复的就先把重复的删掉
	if params.remindId == Constant.FUNC_TYPE_ADD_FRIEND or
		params.remindId == Constant.FUNC_TYPE_GUILD_INVITE or
		params.remindId == Constant.FUNC_TYPE_TEAM_APPLY or
		params.remindId == Constant.FUNC_TYPE_TASK_ROADHERO or
		params.remindId == Constant.FUNC_TYPE_CHAT or
		params.remindId == Constant.FUNC_TYPE_PLAYER_UP or
		params.remindId == Constant.FUNC_TYPE_CHAMPION or
		params.remindId == Constant.FUNC_TYPE_NEWSPAPER or 
		params.remindId == Constant.FUNC_TYPE_CS_GUILD_WAR then

		for i, data in ipairs(self._dataList) do
			if data.remindId == params.remindId then
				if data.remindId == Constant.FUNC_TYPE_PLAYER_UP then
					table.remove(self._dataList, i)
					break
				end
				-- 添加好友
				if data.remindId == Constant.FUNC_TYPE_ADD_FRIEND and data.dataTemp.uid == params.dataTemp.uid then
					table.remove( self._dataList, i )
					break
				end
				-- 邀请入会
				if data.remindId == Constant.FUNC_TYPE_GUILD_INVITE and data.dataTemp.guildId == params.dataTemp.guildId then
					table.remove( self._dataList, i )
					break
				end
                -- 组队申请
                if data.remindId == Constant.FUNC_TYPE_TEAM_APPLY then
                    table.remove( self._dataList, i)
                end
                -- 英雄之路
                if data.remindId == Constant.FUNC_TYPE_TASK_ROADHERO then
                	table.remove(self._dataList, i)
                	break
				end
				-- 密聊新消息
				if data.remindId == Constant.FUNC_TYPE_CHAT then
					table.remove(self._dataList, i)
					break
				end
				-- 冠军赛
				if data.remindId == Constant.FUNC_TYPE_CHAMPION then
					table.remove(self._dataList, i)
					break
				end
				-- 日报
				if data.remindId == Constant.FUNC_TYPE_NEWSPAPER then
					table.remove(self._dataList, i)
					break
				end

				if data.remindId == Constant.FUNC_TYPE_CS_GUILD_WAR then
					table.remove(self._dataList, i)
					break
				end
			end
		end
	end

	if Constant.FUNC_TYPE_CS_GUILD_WAR == params.remindId then
		if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_CS_GUILD_WAR) then
			table.insert( self._dataList, 1, params ) -- 数据队列
		end
	else
		table.insert( self._dataList, 1, params ) -- 数据队列
	end
	if Constant.FUNC_TYPE_TEAM_INVITE == params.remindId or Constant.FUNC_TYPE_TEAM_APPLY == params.remindId then -- 组队邀请?/组队申请
		-- 存在时效性
		self:performWithDelay( function()
			self:removeData( params )
		end, 60 )
	end

	--[[local nodeTemp = self._nodeList[1]
	if not nodeTemp then
		self:_initTipsNode( 1 )
	end
	nodeTemp:getTimeline():gotoFrameAndPlay( 0, true )

	local remindConf = GD:getRemindConfById( params.remindId )
	if remindConf.sound then -- 配置了出现音效？
		-- AM.play(Res., false)
	end

	if remindConf.effect and "" ~= remindConf.effect then -- 配置了帧动画特效？
		nodeTemp:getChildByName( "Panel" ):removeChildByName( "effectNode" )
		local amiNode = cc.CSLoader:createNode( "ui2/main/" .. remindConf.effect .. ".csb" )
		if amiNode then
			amiNode:setName( "effectNode" )
			nodeTemp:getChildByName( "Panel" ):addChild( amiNode, -1 )
			amiNode:getTimeline():gotoFrameAndPlay( 0, true )
		end
	end
	if remindConf.particle and "" ~= remindConf.particle then -- 配置了粒子特效？
	end--]]

	self:_refreshPanel()
end

-- 处理完成之后移除数据
function ClassRef:removeData( params )
	local idx = table.indexof( self._dataList, params )
	if idx then
		table.remove( self._dataList, idx )
		if btnNum >= idx then -- 处于显示范围之内
			self:_refreshPanel()
		end
	end
end

function ClassRef:removeByType( t )
	if self._dataList == nil then
		return
	end

	local rcnt = 0
	for i = #self._dataList, 1, -1 do
		if self._dataList[i].remindId == t then
			table.remove(self._dataList, i)
			rcnt = rcnt + 1
		end
	end

	if rcnt > 0 then
		self:_refreshPanel()
	end
end

return ClassRef
